<template>
  <view>
    <view class="search-box">
      <uni-search-bar @input="input" :radius="100" cancelButton="none" :focus="true">
      </uni-search-bar>
    </view>
    <!-- 搜索建议 -->
    <view class="sugg-list">
      <view class="sugg-item" v-for="item in resultList" :key="item.goods_id" @click="gotoDetail(item)">
        <view class="goods-name">
          {{item.goods_name}}
        </view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box">
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag :text="item" v-for="(item,index) in historyList" :key="index"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  import {
    getError
  } from "@/utils/getError.js"
  export default {
    data() {
      return {
        timer: null,
        resultList: [],
        historyList:['111','222']
      };
    },
    methods: {
      input(e) {
        // 防抖处理
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.getResultList(e)
        }, 500)
      },
      // 获取搜索建议列表
      async getResultList(val) {
        if (val.trim().length === 0) {
          this.resultList = []
        } else {
          const {
            data
          } = await uni.$http.get('/api/public/v1/goods/qsearch', {
            query: val
          })
          getError(data.meta.status)
          this.resultList=data.message
        }
      },
      gotoDetail(item){
        uni.navigateTo({
          url:"/pagesA/goods_detail/goods_detail?goods_id="+item.goods_id
        })
      }
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }
  .sugg-list {
    padding: 0 5px;
    .sugg-item {
      font-size: 12px;
      display: flex;
      padding: 13px 0;
      border-bottom: 1px solid #e7e7e7;
      align-items: center;
      justify-content: space-between;
    }
    .goods-name {
      // 文字不允许换行
      white-space: nowrap;
      // 溢出部分隐藏
      overflow: hidden;
      // 文本溢出后，使用...代替
      text-overflow: ellipsis;
      margin-right: 3px;
    }
  }
  .history-box {
    padding: 0 5px;
  
    .history-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 13px;
      border-bottom: 1px solid #e7e7e7;
    }
  
    .history-list {
      display: flex;
      flex-wrap: wrap;
  margin-top: 5px;
      .uni-tag {
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }
</style>
